package com.ywu.dev.admin.sdk.util;

import java.math.BigInteger;

/**
 * @ClassName StorageConverter
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/11/25 12:39
 * @Version 1.0
 **/
public class StorageConverter {
    private static final BigInteger KB = BigInteger.valueOf(1024);
    private static final BigInteger MB = KB.shiftLeft(10); // 1024 * 1024
    private static final BigInteger GB = MB.shiftLeft(10); // 1024 * 1024 * 1024
    private static final BigInteger TB = GB.shiftLeft(10); // 1024 * 1024 * 1024 * 1024

    public static String convertToHighestDenominator(String value) {
        BigInteger bytes = new BigInteger(value);
        if (bytes.compareTo(TB) > 0) {
            return bytes.divide(TB) + " TB";
        } else if (bytes.compareTo(GB) > 0) {
            return bytes.divide(GB) + " GB";
        } else if (bytes.compareTo(MB) > 0) {
            return bytes.divide(MB) + " MB";
        } else {
            return bytes + " B";
        }
    }
}
